esformatter

ECMAScript code beautifier/formatter.
Important
This tool is still missing support for many important features. Please report
any bugs you find, the code is only as good as the test cases. Feature requests
are very welcome.
We are looking for contributors!!
Why?
jsbeautifier.org doesn't have enough options and not
all IDEs/Editors have a good JavaScript code formatter. I would like to have
a command line tool (and standalone lib) at least as powerful/flexible as the
WebStorm and
FDT code formatters so that it can be plugged
into any editor and reused by other tools like
escodegen.
For more reasoning behind it and history of the project see: esformatter
& rocambole
How?
This tool uses rocambole and
babylon to recursively parse the tokens
and transform it in place.
Goals
- granular control about white spaces, indent and line breaks.
- command line interface (cli).
- be non-destructive.
- support for local/global config file so settings can be shared between team
members.
- be extensive (plugins
and other cli tools).
- support most popular style guides through plugins (Google, jQuery,
Idiomatic.js).
- be the best JavaScript code formatter!
API
var esformatter = require('esformatter');
var fs = require('fs');
var codeStr = fs.readFileSync('path/to/js/file.js').toString();
var options = {
indent : {
value : ' '
},
lineBreak : {
before : {
BlockStatement : '>=1',
DoWhileStatementOpeningBrace : 1,
}
},
whiteSpace : {
}
};
var formattedCode = esformatter.format(codeStr, options);
See the doc/api.md file for a list of all the public methods
and detailed documentation about each one.
See doc/config.md for more info about the configuration
options.
CLI
You can also use the simple command line interface to process the stdin
or
read from a file.
npm install [-g] esformatter
Usage:
Pass the --help
flag to see the available options or see
doc/cli.txt.
esformatter --help
Examples:
esformatter test.js
cat test.js | esformatter
esformatter --config options.json test.js
esformatter test.js > test.out.js
esformatter test.js --indent.value="\t" --lineBreak.before.IfStatementOpeningBrace=0
esformatter -i test.js
esformatter -i 'lib/*.js'
esformatter -i 'lib/**/*.js'
**important:** surround the glob with single quotes to avoid expansion; [glob
syntax reference](https://github.com/isaacs/node-glob/
esformatter --diff test.js
esformatter --diff-unified test.js
esformatter --diff --config options.json test.js
esformatter --diff 'lib/**/*.js'
Local version
If a locally installed esformatter
is found, the CLI uses that instead of the
global executable (this means you can have multiple projects depending on
different versions of esformatter).
protip: add esformatter
and all the plugins that you need on your project
to the package.json devDependencies
that way you can use locally installed plugins and also make sure everyone on
your team is using the same version/settings.
{
"devDependencies": {
"esformatter": "~0.6.0",
"esformatter-quotes": "^1.0.1"
},
"esformatter": {
"plugins": ["esformatter-quotes"],
"quotes": {
"type": "single"
}
}
}
IDE / Editor integration
Since esformatter is available as a command-line tool, it can be used in any editor that supports external shell commands.
Configuration
See doc/config.md.
Pipe other CLI tools
Since we don't expect everyone to write plugins that only works with
esformatter we decided to encourage the usage of standalone CLI tools.
{
"pipe": {
"before": [
"strip-debug",
"./bin/my-custom-script.sh --foo true -zx"
],
"after": [
"baz --keepLineBreaks"
]
}
}
Plugins
Plugins are automatically loaded from node_modules
if you pass the module id
in the config file:
{
"plugins": [ "esformatter-sample-plugin", "foobar" ]
}
List of plugins and plugins wish list:
https://github.com/millermedeiros/esformatter/wiki/Plugins
List of plugins with easy filterable search:
http://pgilad.github.io/esformatter-plugins/
For detailed information about plugins structure and API see
doc/plugins.md
IRC
We have an IRC channel #esformatter on
irc.freenode.net for quick
discussions about the project development/structure.
Wiki
See project Wiki for more info: https://github.com/millermedeiros/esformatter/wiki
Project structure / Contributing
See CONTRIBUTING.md
Popular Alternatives
License
Released under the MIT license